package ar.com.ford.it.fullEconomics.struct;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Vector;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas012VehicleVw;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class AgreementReportVehicleLine {

    private Vector<AgreementReportRow> items;
    private AgreementReportRow lineAverage;
    private BigDecimal volume;
    private String lineName;
    private String volumeDate;
    private Ncas012VehicleVw line;

    public AgreementReportVehicleLine() {
        this.items = new Vector<AgreementReportRow>();
        this.lineAverage = new AgreementReportRow();
        this.volume = new BigDecimal(0);
    }

    // Agrega la linea
    public boolean addRow(AgreementReportRow item) {
        return items.add(item);
    }

    public void calculateTotals() {
        // sumo los amounts
        for (AgreementReportRow row : items) {
            lineAverage.setCurrPrice(lineAverage.getCurrPrice().add(
                    row.getCurrPrice()));
            lineAverage.setFulleAmount(lineAverage.getFulleAmount().add(
                    row.getFulleAmount()));
            lineAverage.setRequestAmount(lineAverage.getRequestAmount().add(
                    row.getRequestAmount()));
            lineAverage.setPayPolAmount(lineAverage.getPayPolAmount().add(
                    row.getPayPolAmount()));
            lineAverage.setLastAgreemAmount(lineAverage.getLastAgreemAmount()
                    .add(row.getLastAgreemAmount()));
        }
        // calculo los porcentajes
        if (lineAverage.getFulleAmount().compareTo(new BigDecimal(0)) != 0)
            lineAverage.setFullePercent(lineAverage.getFulleAmount().subtract(
                    lineAverage.getCurrPrice()).divide(
                    lineAverage.getCurrPrice(), 5, BigDecimal.ROUND_HALF_EVEN)
                    .multiply(new BigDecimal(100)));
        if (lineAverage.getRequestAmount().compareTo(new BigDecimal(0)) != 0)
            lineAverage.setRequestPercent(lineAverage.getRequestAmount()
                    .subtract(lineAverage.getCurrPrice()).divide(
                            lineAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
        if (lineAverage.getPayPolAmount().compareTo(new BigDecimal(0)) != 0)
            lineAverage.setPayPolPercent(lineAverage.getPayPolAmount()
                    .subtract(lineAverage.getCurrPrice()).divide(
                            lineAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));
        if (lineAverage.getLastAgreemAmount().compareTo(new BigDecimal(0)) != 0)
            lineAverage.setLastAgreemPercent(lineAverage.getLastAgreemAmount()
                    .subtract(lineAverage.getCurrPrice()).divide(
                            lineAverage.getCurrPrice(), 5,
                            BigDecimal.ROUND_HALF_EVEN).multiply(
                            new BigDecimal(100)));

    }

    public Vector<AgreementReportRow> getItems() {
        return items;
    }

    public void setItems(Vector<AgreementReportRow> items) {
        this.items = items;
    }

    public AgreementReportRow getLineAverage() {
        return lineAverage;
    }

    public BigDecimal getVolume() {
        return volume;
    }

    public void setVolume(BigDecimal volume) {
        this.volume = volume;
    }

    public String getLineName() {
        return lineName;
    }

    public void setLineName(String lineName) {
        this.lineName = lineName;
    }

    public String getVolumeDate() {
        return volumeDate;
    }

    public void setVolumeDate(String volumeDate) {
        this.volumeDate = volumeDate;
    }
    
    public void setVolumeDate(Timestamp volumeDate) {
        this.volumeDate = FullEconomicsDateUtil.getYear(volumeDate) + " (" + FullEconomicsDateUtil.getMixMonth(volumeDate) + ")FY";
    }

    public Ncas012VehicleVw getLine() {
        return line;
    }

    public void setLine(Ncas012VehicleVw line) {
        this.line = line;
    }

}
